<template>
  <div class="login-container">
    <div class="left-side"></div>
    <div class="right-content">
      <div class="head">
        <!-- <img class="img" src="~@/assets/logo.png" alt="" /> -->
        <h2 class="title-container">登录</h2>
      </div>
      <el-form
        size="large"
        :rules="rules"
        :model="loginForm"
        ref="loginForm"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="用户名"
            v-model="loginForm.username"
            @keyup.enter="focusNext('password')"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            ref="password"
            type="password"
            :prefix-icon="Lock"
            placeholder="密码"
            v-model="loginForm.password"
            @keyup.enter="doLogin"
          />
        </el-form-item>
        <el-button
          type="primary"
          style="width: 100%; margin-top: 20px"
          :loading="loading"
          @click="doLogin"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/store'

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      loading: false
    }
  },
  setup() {
    return {
      User,
      Lock
    }
  },
  methods: {
    focusNext(nextRef) {
      this.$refs[nextRef].focus()
    },

    doLogin() {
      this.$refs['loginForm'].validate(valid => {
        if (valid) {
          this.loading = true
          login(this.loginForm).finally(_ => (this.loading = false))
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  // flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  height: 100vh;
  background: url(~@/assets/background.jpg) center center fixed no-repeat;
  background-size: cover;
  .left-side,
  .right-content {
    flex-basis: 50%;
  }
  .right-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  .head {
    display: flex;
    flex-direction: row;
    margin-bottom: 150px;
    img {
      display: inline-block;
      height: 44px;
      margin-top: 3px;
      border-radius: 50%;
    }
    h2 {
      margin: 0 5px;
      color: rgb(227, 78, 78);
      font-size: 44px;
      text-align: center;
    }
  }
  .login-form {
    position: relative;
    top: -50px;
    width: 420px;
  }

  :deep(.el-form-item) {
    color: #0a0a0a;
    background: rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
  }

  :deep(.el-input__inner) {
    color: rgb(0, 0, 0);
    background-color: transparent;
  }
}
</style>
